#ifndef DOMAINNAMESYSTEM_H
#define DOMAINNAMESYSTEM_H

#include<QString>
#include<QStringList>
#include<QTextStream>
#include<QDebug>

class DomainNameSystem{
public:
    struct DomainInfo{
        QString name;
        QString IP;
        QString owner;
        QString notes;

        const static int dataNum=4;

        DomainInfo(const QString &name,const QString &IP,const QString &owner,const QString &notes);
        DomainInfo(const DomainInfo &info);
    };

    DomainNameSystem();
    ~DomainNameSystem();

    static DomainNameSystem& DNS();

    static bool isValidDomain(const QString &domain);

    void insertDomain(const QString &domain,const DomainInfo &info);

    void displayDomains();

    void saveDomains(QTextStream &out);

    void readDomains(QTextStream &in);

private:
    struct DomainNode{
        QString name;

        DomainNode *firstSon;
        DomainNode *rightBro;

        DomainInfo *info;

        DomainNode(const QString &name);
    };

    DomainNode *root;

    void deleteTree(DomainNode* node);

    void addDomainToTree(const QString &domain,const DomainInfo &info,DomainNode* node);

    void inOrderTraversal(DomainNode* node);

    void writeDomainToFile(QTextStream &out,DomainNode* node);

    void readDomainFromFile(QTextStream &in,DomainNode* node);
};

#endif // DOMAINNAMESYSTEM_H
